<template>
  <div>
    <div class="his-hot" v-show="isShowHistory">
      <div class="hd">
        <h4>历史记录</h4>
        <van-icon size=".2rem" name="delete-o" @click="removeHistory" />
      </div>
      <div class="bd">
        <van-tag
          plain
          v-for="(item, index) in historyKeywordList"
          :key="index"
          @click="tagSearch(item)"
          >{{ item }}</van-tag
        >
      </div>
    </div>
    <div class="his-hot">
      <div class="hd">
        <h4>热门搜索</h4>
      </div>
      <div class="bd">
        <van-tag
          plain
          v-for="(item, index) in hotKeywordList"
          :type="item.is_hot ? 'warning' : 'default'"
          :key="index"
          @click="tagSearch(item.keyword)"
          >{{ item.keyword }}</van-tag
        >
      </div>
    </div>
  </div>
</template>

<script>
import { removeSearchHistory } from "../request/api"
export default {
  name: "HistoryHot",
  props: {
    historyKeywordList: {
      type: Array,
    },
    hotKeywordList: {
      type: Array,
    },
  },
  data() {
    return {
      isShowHistory: true,
    }
  },
  methods: {
    // (历史和热门)点击标签执行搜索(子传父)
    tagSearch(val) {
      // console.log(val)
      this.$emit("tagSearch", val)
    },
    // 清除历史
    removeHistory() {
      removeSearchHistory().then((res) => {
        if (res.errno === 0) {
          this.$toast.success("删除成功")
          // 隐藏历史记录的盒子
          setTimeout(() => {
            this.isShowHistory = false
          }, 1000)
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.his-hot {
  margin-bottom: 0.2rem;
  background-color: white;
  padding: 0.1rem;
  .hd {
    display: flex;
    justify-content: space-between;
    h4 {
      font-size: 0.22rem;
    }
    margin-bottom: 0.15rem;
  }
  .bd {
    .van-tag {
      margin-right: 0.1rem;
      margin-bottom: 0.05rem;
      padding: 0.02rem 0.05rem;
    }
  }
}
</style>
